home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
007
/
tbbyte.arc
/
QSORT.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1985-08-14
|
5KB
|
159 lines
Program Quicksort(Input,Outupt);
{ An interactive quicksort program in TURBO PASCAL. This program
is presented as an example of how to do quicksort, and is hence
interactive. You can easily extract the main routine (function QSRT)
and the type
definition for NumberPointer and see how this program constructs
the simple linked list, so your programs can use it as a function.
Running this program:
Run in TURBO and it will prompt for numbers. Input numbers to sort
each followed by a newline (or return). End input of numbers with
an entry of -11111.
The program will then, almost instantaneously, spit out the list in
sorted order.
On QUICKSORT:
QUICKSORT was invented in the early 60's by C.A.R. (Tony) Hoare. It is
the best sorting algorithm known (although a version called QUICKERSORT
is faster for certain cases). The algorithm revolves around breaking a
list into sublists. A number (say the first in the list) is chosen. Numbers
smaller than that number are placed on a list (conceptually the LEFT list),
and numbers greater than or equal are placed on another list (conceptually
the RIGHT list). The process is then repeated, recursively, on the
sub-lists until there is only one element on each list, when they are
joined in sorted order.
Example:
LIST: (3 5 1 2 4)
break on 3 / \
Sublists (1 2) (3 5 4)
do it again: / \ / \
stop when there is (1) (2) (3) (5 4)
only one element |------|-----|\ / \
in each list: \ (4) (5)
et voila, the 'bottom' ---|----|
nodes of the tree are sorted.
The number of comparisons at each level drops off very quickly. For
further explanation see Knuth, The Art Of Computer Progammingm Vol. 3
'Sorting and Searching'.
APOLOGY
The program is uncommented. I guess I'm just lazy...sigh...
And Borland charges for this!?!}
Type
Number_ptr = ^Number_rec;
Number_rec = record
number: Integer;
next: Number_ptr;
end;
Var
First_number, New_number, Last_number: Number_ptr;
InNumber: Integer;
Procedure PrintList(List:Number_ptr);
begin
writeln;
while (List<>nil) do
begin
writeln(List^.number);
List:=List^.next;
end;
end;
Function QSRT(List:Number_ptr): Number_ptr;
Var
First_hi, Last_hi, New_number, First_lo, Last_lo: Number_ptr;
Compare_number: Integer;
begin
If List=nil then
QSRT:=List
else
begin
New(First_hi);
Last_hi:=First_hi;
New(First_lo);
Last_lo:=First_lo;
Compare_number:=List^.number;
List:=List^.next;
while List<>nil do
begin
if List^.number<Compare_number then
begin
New(New_number);
New_number^.number:=List^.number;
New_number^.next:=nil;
Last_lo^.next:=New_number;
Last_lo:=New_number;
end
else
begin
New(New_number);
New_number^.number:=List^.number;
New_number^.next:=nil;
Last_hi^.next:=New_number;
Last_hi:=New_number;
end;
List:=List^.next;
end;
First_lo:=QSRT(First_lo^.next);
First_hi:=QSRT(First_hi^.next);
If First_lo<>nil then
begin
Last_lo:=First_lo;
while (Last_lo^.next<>nil) do Last_lo:=Last_lo^.next;
New(New_number);
New_number^.number:=Compare_number;
New_number^.next:=First_hi;
Last_lo^.next:=New_number;
end
else
begin
New(First_lo);
First_lo^.number:=Compare_number;
First_lo^.next:=First_hi;
end;
QSRT:=First_lo;
end;
end; {QSRT}
begin
First_number:=nil;
Write('Number?');
Read(InNumber);
while(InNumber<>-11111) do
begin
New(New_number);
New_number^.number:=InNumber;
Writeln;
If First_number = nil then
First_number:=New_number
else
Last_number^.next:=New_number;
Last_number:=New_number;
Last_number^.next:=nil;
Write('Number?');
Read(InNumber);
end;
First_number:=QSRT(First_number);
PrintList(First_number);
end.it will prompt for numbers. Input numbers to sort
each foll